package com.avic.rong.goodsSort.adapter.secondListAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllTsBean;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 */
public class GoodsSortTsShowRvAdapter extends RecyclerView.Adapter<GoodsSortTsShowRvAdapter.GoodsSortTsShowRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllTsBean.DataBean> beanList = new ArrayList<>();
    private GoodsTsRvListener goodsTsRvListener;


    public void setGoodsTsRvListener(GoodsTsRvListener goodsTsRvListener) {
        this.goodsTsRvListener = goodsTsRvListener;
    }

    public GoodsSortTsShowRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setBeanList(List<GoodsSortSearchAllTsBean.DataBean> beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodsSortTsShowRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_goods_sort_ts_rv, viewGroup, false);
        return new GoodsSortTsShowRvViewHolder(view);
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull GoodsSortTsShowRvViewHolder holder, int i) {


        GoodsSortSearchAllTsBean.DataBean bean = beanList.get(i);
        holder.title.setText(beanList.get(i).getTitle());
        DecimalFormat df = new DecimalFormat("###0.00");
        String format = df.format(beanList.get(i).getPrice());
        if (StringUtils.isEmpty(beanList.get(i).getImg())) {
            holder.layout.setVisibility(View.VISIBLE);
            holder.img.setVisibility(View.GONE);
            holder.down.setText(beanList.get(i).getSource());
            holder.up.setText(beanList.get(i).getNo());
        }else{
            holder.layout.setVisibility(View.GONE);
            Glide.with(context).load(beanList.get(i).getImg()).placeholder(R.mipmap.doc_default).into(holder.img);
        }

        holder.desc.setText(beanList.get(i).getDesc());
        holder.price.setText("¥" + format);
        holder.car.setOnClickListener(new MyClickListener(holder, bean));
//        holder.car.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                String s = beanList.get(i).getUrl();
//                String[] split = s.split("/");
//                Log.e("cjx", "splite:" + split[2]);
//                String skuStr = "{" + "\"" + split[2].toString() + "\"" + ":" + "1}";
//                goodsTsRvListener.addTsCar(skuStr);
//            }
//        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = beanList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);

                Intent intent = DetailProductActivity.getIntent(context, split[2]);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }


    class MyClickListener implements View.OnClickListener {
        GoodsSortTsShowRvViewHolder holder;
        GoodsSortSearchAllTsBean.DataBean bean;

        public MyClickListener(GoodsSortTsShowRvViewHolder holder, GoodsSortSearchAllTsBean.DataBean bean) {
            this.holder = holder;
            this.bean = bean;
        }

        @Override
        public void onClick(View v) {
            String s = bean.getUrl();
            String[] split = s.split("/");
            Log.e("cjx", "splite:" + split[2]);
            String skuStr = "{" + "\"" + split[2].toString() + "\"" + ":" + "1}";
            goodsTsRvListener.addTsCar(skuStr, holder);
        }
    }

    public static class GoodsSortTsShowRvViewHolder extends RecyclerView.ViewHolder {
        TextView title, desc, price;
        public ImageView car;
        ImageView img;
        TextView down,up;
        LinearLayout layout;
        public GoodsSortTsShowRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.goods_sort_ts_rv_img);
            price = itemView.findViewById(R.id.goods_sort_ts_rv_price);
            title = itemView.findViewById(R.id.goods_sort_ts_rv_title);
            car = itemView.findViewById(R.id.goods_sort_ts_rv_car);
            desc = itemView.findViewById(R.id.goods_sort_ts_rv_desc);
            layout = itemView.findViewById(R.id.goods_wd_text_layout);
            down = itemView.findViewById(R.id.goods_wd_text_down);
            up = itemView.findViewById(R.id.goods_wd_text_up);



        }
    }

    public interface GoodsTsRvListener {
        public void addTsCar(String skuStr, GoodsSortTsShowRvViewHolder holder);
    }
}
